package org.video.mine.model;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.video.mine.MyPreference;
import org.video.mine.Urls;
import org.video.mine.activity.LoadDetailActivity;
import org.video.mine.activity.MovieDetailActivity;
import org.video.mine.activity.PlayLoadedActivity;
import org.video.mine.activity.PlayerActivity;
import org.video.mine.activity.SearchActivity;
import org.video.mine.entity.LoadedEntity;
import org.video.mine.entity.LookHistoryEntity;
import org.video.mine.event.ChangePage;
import org.video.mine.event.DeleteEvent;
import org.video.mine.event.RefreshHistoryEvent;

import java.io.File;
import java.util.Collections;
import java.util.List;

public class ClickModel {
    public void clickMore(String moveType){
        Log.d("点击类型=",moveType);
        if (moveType.contains("电影")){
            EventBus.getDefault().post(new ChangePage(1));
        }else if (moveType.contains("电视")){
            EventBus.getDefault().post(new ChangePage(2));
        }else if (moveType.contains("综艺")){
            EventBus.getDefault().post(new ChangePage(4));
        }else if (moveType.contains("动漫")){
            EventBus.getDefault().post(new ChangePage(3));
        }
    }

    public void clickMovieInfo(String movieUrl, Context context){
        Log.d("电影类型==",movieUrl);
        Intent intent = new Intent(context, MovieDetailActivity.class);
        intent.putExtra("url", movieUrl.startsWith(Urls.getHomeUrl0())?movieUrl:(Urls.getHomeUrl0() +movieUrl));
        context.startActivity(intent);
    }

    public void clickSearch(View view){
        view.getContext().startActivity(new Intent(view.getContext(), SearchActivity.class));
    }

    public void playHistory(LookHistoryEntity entity, Context context){
        if (entity.isLoaded()){
            String path = entity.getPlayUrl();
            File file = new File(path);
            if (file.exists()) {
                Intent intent = new Intent(context, PlayLoadedActivity.class);
                intent.putExtra("path", path);
                String name = entity.getName();
                name = name.substring(name.indexOf("(") + 1, name.indexOf(")"));
                intent.putExtra("set", name);
                context.startActivity(intent);
            }else {
                Toast.makeText(context,"文件不存在",Toast.LENGTH_SHORT).show();
                deleteHistory(entity,context);
            }
        }else {
            Bundle bundle = new Bundle();
            bundle.putString("url", entity.getPlayUrl());
//                bundle.putSerializable("play", (Serializable) playerEntities);
            bundle.putInt("which", entity.getWhich());
            bundle.putString("imgUrl", entity.getImgUrl());
            bundle.putString("name", entity.getName());
            bundle.putInt("time", entity.getPlayPosition());
            bundle.putInt("position", entity.getPosition());
            Intent intent = new Intent(context, PlayerActivity.class);
            intent.putExtra("bundle", bundle);
            context.startActivity(intent);
        }
//        context.startActivity(new Intent(context, PlayTestActivity.class));
    }

    public void deleteHistory(LookHistoryEntity entity, Context context){
        MyPreference preference = MyPreference.getInstance();
        preference.setPreference(context);
        List<LookHistoryEntity> historyEntities = (List<LookHistoryEntity>) preference.getObject(MyPreference.history, LookHistoryEntity.class);
        int which=0;
        for (int i=0;i<historyEntities.size();i++){
            LookHistoryEntity item = historyEntities.get(i);
            if (item.getName().equals(entity.getName())){
                which = i;
                break;
            }
        }
        int pos = historyEntities.size()-which-1;
        historyEntities.remove(which);
        preference.setObject(MyPreference.history,historyEntities);
        EventBus.getDefault().post(new DeleteEvent(pos));
    }

    public void showLoadDetail(Context context, LoadedEntity entity, boolean isLoading){
        Intent intent = new Intent(context, LoadDetailActivity.class);
        intent.putExtra("path",entity.getFileDir());
        intent.putExtra("isLoading",isLoading);
        context.startActivity(intent);
    }

    public void playLoaded(Context context, LoadedEntity entity){
        Intent intent = new Intent(context, PlayLoadedActivity.class);
        intent.putExtra("path",entity.getFileDir());
        intent.putExtra("set",entity.getName());
        context.startActivity(intent);
    }

}
